569C - Primes or Palindromes - CodeForces Solution


binary search brute force math number theory *1600

Please click on ads to support us..

C++ Code:

#define aavade(a,n) for(int i=0;i<n;i++) cin>>a[i]
#define f(i,x,n) for(int i=x;i<n;i++)
#define bf(i,n,x) for(int i=n-1;i>=x;i--)
#define ll long long int
#define Nottle    std::ios::sync_with_stdio(false);
#define Max 1000000000+7
#define ull unsigned long long int
#include<bits/stdc++.h>
using namespace std;
int pal[2000001];
int prime[2000001];
vector <int> factors(int x)
{
vector <int> result;
int i = 1;
while(i*i <= x) {
if(x % i == 0) {
result.push_back(i);
if(x/i != i) {
result.push_back(x/i);
}
}
i++;
}
sort(result.begin(),result.end());
return result;
}
ll fact(ll n)
{
if(n==1 ||n==0)
return 1;
else
return n*fact(n-1);
}
bool isPrime (int number) { 
    if (number < 2) return false;
    if (number == 2) return true;
    if (number % 2 == 0) return false;
    for (int i=3; (i*i) <= number; i+=2) {
        if (number % i == 0 ) return false;
    }
    return true;
}
bool isPal(int num)
{
    string s=to_string(num);
    string s1=s;
    reverse(s.begin(),s.end());
    if(s1==s) return true;
    else return false;
}
string getbinary(int x)
{
string s;
bf(i,10,0)
{
s+=to_string((x>>i)&1);
}
return s;
}
int rev (int x){
int res = 0;
while (x){ res = 10*res+x%10;
x/=10;
}
return res;
}
int main()
{
Nottle
prime[2]=1;
f(i,3,1534678)
{
    if(isPrime(i))
    prime[i]=prime[i-1]+1;
    else prime[i]=prime[i-1];
}
 pal[1]=1;
f(i,2,1534567)
{
    if(rev(i)==i)
    pal[i]=pal[i-1]+1;
    else pal[i]=pal[i-1];
}
int t;
t=1;
while(t--)
{
    int p,q;
    cin>>p>>q;
    int fl=0;
    int i;
    // cout<<prime[100000];
    for(i=1;i<=1534567;++i)
    {
       if(q*prime[i]<=p*pal[i])
        {
            fl=i;
            // cout<<i<<" ";
        }
    }
    // cout<<pal[40]<<" "<<prime[40];
    if(fl) cout<<fl;
    else cout<<"Palindromic tree is better than splay tree";
    //cout<<'\n';
}
return 0;
}


Comments

Submit
0 Comments
More Questions

1302. Deepest Leaves Sum
1209. Remove All Adjacent Duplicates in String II
994. Rotting Oranges
983. Minimum Cost For Tickets
973. K Closest Points to Origin
969. Pancake Sorting
967. Numbers With Same Consecutive Differences
957. Prison Cells After N Days
946. Validate Stack Sequences
921. Minimum Add to Make Parentheses Valid
881. Boats to Save People
497. Random Point in Non-overlapping Rectangles
528. Random Pick with Weight
470. Implement Rand10() Using Rand7()
866. Prime Palindrome
1516A - Tit for Tat
622. Design Circular Queue
814. Binary Tree Pruning
791. Custom Sort String
787. Cheapest Flights Within K Stops
779. K-th Symbol in Grammar
701. Insert into a Binary Search Tree
429. N-ary Tree Level Order Traversal
739. Daily Temperatures
647. Palindromic Substrings
583. Delete Operation for Two Strings
518. Coin Change 2
516. Longest Palindromic Subsequence
468. Validate IP Address
450. Delete Node in a BST